<template>
    <el-dialog :close-on-click-modal="false" title="充值" :visible.sync="visible" width="32.5%" :before-close="handleClose">
        <el-form
          ref="rechargeForm" 
          :model="form" 
          :rules="rules" 
          status-icon
          @submit.native.prevent
        >
          <el-form-item prop="recharge" label="充值金额">
            <el-input v-model="form.recharge" placeholder="金额" @keyup.enter.native="false"/>
          </el-form-item>
          <div>请选择您的付款方式: </div>
        </el-form>
    <template #footer>
      <div class="buttons">
        <el-button type="primary" @click="handleRecharge">确 定</el-button>
        <el-button plain @click="handleClose">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { mapActions, mapState } from 'vuex';

export default {
    name:'' ,
    components: {},
    props: {
        visible: {
            type: Boolean,
            default: false
        }
    },
    computed: {
        ...mapState({
            userInfo: state => state.user.userInfo
        }),
    },
    data() {
        return {
            form:{ 
                recharge: ''
            },
            rules: {
                //数组里一个对象对应套规则(包括检验内容,提示语)
                recharge: [
                    { required: true, message: '充值金额不能为空', trigger: 'change' },
                ],
            },
        };
    },
    methods: {
        ...mapActions({
            fetchRecharge: 'user/fetchRecharge'
        }),
        handleClose(){
            Object.keys(this.form).forEach(key => {
                this.form[key] = null;
            });
            this.$emit('close');
        },
        handleRecharge(){
            this.$refs.rechargeForm.validate((valid) => {
                if(valid){
                    this.fetchRecharge({
                        user_id: this.userInfo.user_id,
                        recharge: this.form.recharge
                    });
                    this.handleClose();
                }else{
                return false;
                }
            })
        }
    },
}
</script>
<style lang='scss' scoped src="./index.scss"/>